#include "gpio.h"

void hal_gpio_init(gpio_t *gpio, unsigned int pin, altfn_t alt)
{
	int p;
	// 判断操作的是哪个引脚
	for (p = 0; p < 32; p++)
	{
		if (pin & (0x1 << p))
		{
			if (p < 16) {
				gpio->ALTFN0 &= (~(0x3 << (p * 2))); 
				gpio->ALTFN0 |= (alt << (p * 2));
			} else {
				gpio->ALTFN1 &= (~(0x3 << ((p%16) * 2)));
				gpio->ALTFN1 |= (alt << ((p%16) * 2));
			}
		}
	}


}
void hal_gpio_mode(gpio_t *gpio, unsigned int pin, mode_t mode)
{
	if (mode)
	{
		gpio->OUTENB |= pin;
	} else {
		gpio->OUTENB &= (~pin);
	}
}
void hal_gpio_write(gpio_t *gpio, unsigned int pin, level_t level)
{
	if (level)
	{
		gpio->OUT |= pin;
	} else {
		gpio->OUT &= (~pin);
	}
}
level_t hal_gpio_read(gpio_t *gpio, unsigned int pin)
{
	if (gpio->PAD & (pin)) 
	{
		return HIGH_LEVEL;
	} else {
		return LOW_LEVEL;
	}

}
